﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InterviewQuestions.LargestSum
{
    public class LargestSum
    {
        public int Execute(int[] series)
        {
            if (series == null)
            {
                return 0;
            }

            //doesn't work when input series are all negative.
            int maxsofar = 0;
            int maxhere = 0;

            for (int i = 0; i < series.Length; i++)
			{
                maxhere = Math.Max(maxhere + series[i], 0);
                maxsofar = Math.Max(maxsofar, maxhere);
			}

            return maxsofar;
        }
    }
}
